#pragma once
#include<iostream>
#include<algorithm>
#include <vector>
using namespace std;

namespace lth
{
	template <class T, class container = vector<T>>
	class priority_queue
	{
	public:

		void adjust_up(int child)
		{
			int father = (child - 1) / 2;
			while (child > 0)
			{
				if (_con[child] > _con[father])
				{
					swap(_con[child], _con[father]);
					child = father;
					father = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}


		void adjust_down(int father)
		{
			int child = father * 2 + 1;
			while (child + 1 < _con.size())
			{
				if ((_con[child] < _con[child + 1]) && child + 1 < _con.size() - 1) child++;
				if (_con[father] < _con[child])
				{
					swap(_con[child], _con[father]);
					father = child;
					child = father * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}

		void push(const T& x)
		{
			_con.push_back(x);
			adjust_up(_con.size() - 1);
		}

		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			adjust_down(0);
		}

		const T& top()
		{
			return _con[0];
		}

		size_t size()
		{
			return _con.size();
		}

		bool empty()
		{
			return _con.empty();
		}

	private:
		container _con;
	};


	void test1()
	{
		priority_queue<int> pq;
		pq.push(1);
		pq.push(0);
		pq.push(7);
		pq.push(4);
		pq.push(2);
		pq.push(1);

		while (!pq.empty())
		{
			cout << pq.top() << " ";
			pq.pop();
		}
		cout << endl;
	}

}